
package com.probiz.estore.core.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;

import com.probiz.estore.core.util.SearchWordKey;

public class SearchWordFilter extends OncePerRequestFilter {

	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws ServletException, IOException {
		chain.doFilter(request, response);
		String uri = request.getRequestURI();
		uri = uri.substring(uri.lastIndexOf("/") + 1);
		String key = SearchWordKey.getKey(uri);
		if (key == null)
			return;
		if (SearchWordKey.getObjName(key) == null) {
			return;
		}
		Object results = request.getAttribute(SearchWordKey.getObjName(key));
		List list = new ArrayList();
		try {
			list = (List) results;
		} catch (Exception e) {
		}
		if (list == null)
			return;
		if (list.size() == 0)
			return;
		String filed = SearchWordKey.getFiled(key);
		String searchWord = request.getParameter(filed);

		HttpSession session = request.getSession();
		Object searchObj = session.getAttribute("searchWords");
		if (searchObj == null) {
			Set words = new HashSet();
			try {
				words.add(searchWord);
			} catch (Exception e) {
			}
			session.setAttribute("searchWords", words);
			return;
		}
		if (searchObj instanceof Set) {
			Set temp = (Set) searchObj;
			try {
				temp.add(searchWord);
			} catch (Exception e) {
			}
			session.setAttribute("searchWords", temp);
		}

	}

}
